package com.example.tbag.healthcare.Activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.EditText;

import com.example.tbag.healthcare.R;
import com.example.tbag.healthcare.Util.CallPhoneUtil;
import com.example.tbag.healthcare.Util.DatabaseHelper;
import com.example.tbag.healthcare.Util.ToastUtil;

import java.util.List;

import butterknife.BindViews;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Tbag on 2017/4/21.
 */

public class AddPersonActivity extends AppCompatActivity {
    @BindViews({R.id.edit1, R.id.edit2, R.id.edit3, R.id.edit4, R.id.edit5, R.id.edit6,R.id.edit7})
    List<EditText> editTexts;
    private DatabaseHelper helper;

    @OnClick(R.id.btn_back)
    public void back() {
        this.finish();
    }

    @OnClick(R.id.btn1)
    public void exit() {
        this.finish();
    }

    @OnClick(R.id.btn2)
    public void checkAndSave() {
        int index = 0;
        for (EditText editText : editTexts) {
            if (editText.getText().toString().trim().isEmpty()) {
                switch (index) {
                    case 0:
                        ToastUtil.show(this, "姓名不能为空");
                        return;
                    case 1:
                        ToastUtil.show(this, "性别不能为空");
                        return;
                    case 2:
                        ToastUtil.show(this, "年龄不能为空");
                        return;
                    case 3:
                        ToastUtil.show(this, "科室不能为空");
                        return;
                    case 4:
                        ToastUtil.show(this, "床号不能为空");
                        return;
                    case 5:
                        ToastUtil.show(this, "住院号不能为空");
                        return;
                    case 6:
                        ToastUtil.show(this, "已缴费金额不能为空");
                        return;
                }
            }
            index++;
        }
        helper.getWritableDatabase().execSQL("insert into people" +
                "(name,sex,age,type,bedNum,doc,givenMoney ,usedMoney ,leftMoney)" +
                "values(?,?,?,?,?,?,?,?,?)", new Object[]{editTexts.get(0).getText().toString(), editTexts.get(1).getText().toString(), editTexts.get(2).getText().toString(),
                editTexts.get(3).getText().toString(), editTexts.get(4).getText().toString(), editTexts.get(5).getText().toString(),editTexts.get(6).getText().toString(), "0",editTexts.get(6).getText().toString(), null, null, null, null, null, null, null, null, null});
        ToastUtil.show(this, "保存成功");
        new Handler(getMainLooper()).postDelayed(new Runnable() {
            @Override
            public void run() {
                exit();
            }
        }, 800);
    }

    @OnClick(R.id.right_btn)
    public void callForHelp() {
        CallPhoneUtil.call(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_person);
        ButterKnife.bind(this);
        helper = new DatabaseHelper(this);
    }
}
